/*
 * @(#)NateOnMessage.java
 *
 * Copyright (c) 1998-2005 Jang-Ho Hwang, 
 * All Rights Reserved.
 *
 * $Id: NateOnMessage.java,v 1.1.1.1 2005/05/22 01:02:27 rath Exp $
 */
package rath.nateon;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * 네이트온 메시지.
 * 
 * @author Jang-Ho Hwang, rath@xrath.com
 * @version 1.0.000, 2005/05/20
 */
public class NateOnMessage 
{
	private int trId;
	private String head;
	private ArrayList list = new ArrayList(4);

	private String cbMethod = null;
	private Object cbObject = null;

	public NateOnMessage()
	{

	}

	public NateOnMessage( String head )
	{
		setHeader( head );
	}

	public String getHeader()
	{
		return this.head;
	}

	public void setHeader( String head )
	{
		this.head = head;
	}

	public void setTrId( int id )
	{
		this.trId = id;
	}

	public int getTrId()
	{
		return this.trId;
	}

	public void setCallback( String cbMethod )
	{
		this.cbMethod = cbMethod;
	}

	public void setCallback( String cbMethod, Object param )
	{
		this.cbMethod = cbMethod;
		this.cbObject = param;
	}

	public String getCallback()
	{
		return this.cbMethod;
	}

	public Object getCallbackObject()
	{
		return this.cbObject;
	}

	public NateOnMessage add( String param )
	{
		list.add(param);
		return this;
	}

	public NateOnMessage add( int param )
	{
		return add( String.valueOf(param) );
	}

	public NateOnMessage add( long param )
	{
		return add( String.valueOf(param) );
	}

	public String get( int index )
	{
		return (String)list.get(index);
	}

	public int getInt( int index )
	{
		String value = get(index);
		return Integer.parseInt(value);
	}

	public long getLong( int index )
	{
		String value = get(index);
		return Long.parseLong(value);
	}

	public int size()
	{
		return list.size();
	}

	public byte[] getBytes() throws IOException
	{
		if( head==null )
		{
			throw new IllegalStateException("Header not set yet");
		}
		StringBuffer sb = new StringBuffer();
		sb.append( head );
		sb.append( ' ' );
		sb.append( trId );
		for(int i=0; i<list.size(); i++)
		{
			sb.append(' ');
			sb.append( list.get(i) );
		}
		sb.append( "\r\n" );

		return sb.toString().getBytes("MS949");
	}

	public static NateOnMessage create( byte[] b ) throws IOException
	{
		String str = new String(b, "MS949");//, "UTF-8");
		String[] param = str.split(" ");
		int offset = 2;

		NateOnMessage msg = new NateOnMessage();
		msg.setHeader( param[0] );
		if( msg.head.equals("INVT") )
		{
			offset = 1;
		}
		else
		{
			msg.setTrId( Integer.parseInt(param[1]) );
		}

		for(int i=0; i<param.length-offset; i++)
		{
			msg.add( param[offset+i] );
		}
		return msg;
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append( head );
		sb.append( ' ' );
		sb.append( trId );
		for(int i=0; i<list.size(); i++)
		{
			sb.append(' ');
			sb.append( list.get(i) );
		}
		return sb.toString();
	}
}
